define(window.backboneCommon.getViewDefine("campaign.index"), function ($,require,Collection,tpl) {
    var backboneView= Backbone.View.extend({
        events: {
            "click .global-listParam":"saveGlobalListParam"
        },
        initialize: function () {
            this.modelAttach={};
            this.modelAttach.customer=this.model.get("modelCustomer");
            this.listenTo(this.modelAttach.customer, 'bind:AllList', this.bindCustomer);
            this.listenTo(this.model, 'bind:PageList', this.bindList);
            this.render();
        },
        render:function(){
            var templateHtml=_.template(tpl);
            this.$el.html(templateHtml());
            return this;
        },
        initMain:function(){
            this.elementModel={};
            this.extObject={};
            this.pageData={listParam:{},initExtObject:{}};
            this.elementModel.table=$("#tb_CampaignList");
            this.elementModel.timeRange=$("#search_campaignTimeRange");
            $.ExtAssist.navBread.nav([{name:"投放管理"},{name:"订单管理"}]);
            this._initPagePower();
        },
        _initPagePower:function(){
            var self=this;
            $.ExtAssist.rolePower.operatePage("list",["advertiserList","create","select","edit"],function(json){
                self.pageData.power={
                    isEdit:json["edit"],
                    isOnlyView:json["select"],
                    isAdvertiser:json["advertiserList"]
                };
                if(self.pageData.power.isEdit){self.pageData.power.isOnlyView=false;}
                self._initBodyMain();
                if(json["advertiserList"]){self.collection.getCustomer()}
            });
        },
        _initBodyMain:function(){
            this._initAssistCallback();
            this._getGlobalListParam();
            this._initTimeRange();
            this._initInputSearch();
            this._initPageList();
        },
        _initAssistCallback:function(){
            var self=this;
            $.ExtAssist.winUnload.callback(function(){
                self._setGlobalListParam(null)
            });
        },

        _getGlobalListParam:function(){
            var data=$.ExtAssist.pageListParam.getData("campaignList");
            if(data.search){$("#panel_inputSearch").data("initValue",data.search)}
            if(data.customer){$("#search_customer").data("initValue",data.customer)}
            if(data.startTime){this.elementModel.timeRange.data("startValue",data.startTime)}
            if(data.endTime){this.elementModel.timeRange.data("endValue",data.endTime)}
            this.pageData.listParam=data;
        },
        _setGlobalListParam:function(param){
            $.ExtAssist.pageListParam.setData("campaignList",param);
        },
        saveGlobalListParam:function(){
            var param=$.ExtCommon.getJsonClone(this.pageData.listParam);
            param.offset=this.extObject.table.getCurrentOffset();
            this._setGlobalListParam(param);
        },

        _initTimeRange:function () {
            var self=this;
            var options={type:"range", changeCallback: function () {self.refreshList()}};
            if($.ExtCommon.isCheckUndefined(this.pageData.listParam.startTime) && $.ExtCommon.isCheckUndefined(this.pageData.listParam.endTime)){
                options.startValue=$.ExtMoment.initAdd(-1);
                options.endValue=$.ExtMoment.initAdd(-1);
            }
            self.extObject.timeRange=self.elementModel.timeRange.ExtDatePicker(options);
        },
        _initInputSearch:function () {
            var self=this;
            self.extObject.inputSearch=$("#panel_inputSearch").ExtInputSearch({
                placeholder:"订单ID,订单名称",
                searchCallback:function () {
                    self.refreshList()
                }
            })
        },
        _getTimeRangeValue:function(){
            var arrTime =this.extObject.timeRange.getValue();
            return {startTime:arrTime[0],endTime:arrTime[1]}
        },

        bindCustomer:function () {
            var self=this;
            var res=self.modelAttach.customer.get("dataAllList");
            var list=$.ExtCommon.convertListByPara(res.data,{name:"nameExt"});
            $("#search_customer").ExtSelect2({placeholder: "-广告主-",allowClear: true,minimumResultsForSearch: 1, infoExt:{data:list,
                initCallback:function(){
                    self.pageData.initExtObject.customer=true;
                },
                changeCallback:function(){self.refreshList()}
            }});
            if(!res.success){ $.ExtNotify.error("广告主数据获取失败："+res.msg);}
        },
        _getSearchParam:function(){
            var param={};
            var keyword = this.extObject.inputSearch.getValue();
            if (!$.ExtCommon.isCheckEmpty(keyword)) {param.search = keyword;}
            if(this.pageData.power.isAdvertiser){
                var customer="";
                if(this.pageData.initExtObject.customer){
                    customer=$("#search_customer").val();
                }else{
                    if(!$.ExtCommon.isCheckEmpty(this.pageData.listParam.customer)){customer=this.pageData.listParam.customer}
                }
                if (!$.ExtCommon.isCheckEmpty(customer)) {param.customer = customer;}
            }
            var timeRange=this._getTimeRangeValue();
            param= $.ExtCommon.initMergeJson(param,timeRange);
            return param;
        },
        _initPageList: function () {
            var self=this;
            var options={
                columns: [
                    {data: "id", class: "w-60"},
                    {data: "name",class:"p-r-20"},
                    {data: "ratioBudget",class: "w-110"},
                    {data: "pv",class: "w-110 text-right num-int"},
                    {data: "click",class: "w-110 text-right num-int"},
                    {data: "cr", class: "w-110 text-right num-float2"},
                    {data: "unitPrice", class:"w-110 text-right num-float2"},
                    {data: "cost", class:"w-110 text-right num-float2"}
                ],
                infoExt:{
                    ajaxDataPara:function(){
                        var param= {};
                        param= $.ExtCommon.initMergeJson(param,self._getSearchParam());
                        self.pageData.listParam = $.ExtCommon.getJsonClone(param);
                        return param;
                    },
                    ajaxWholeCallback:function(param) {
                        self.collection.getPageList(param)
                    }
                },
                createdRow: function (row, data) {
                    data.operateEditName=self.pageData.power.isEdit?"编辑":(self.pageData.power.isOnlyView?"查看":"");
                    var elementTd=$('td', row);
                    elementTd.eq(1).html($("#template_name").render(data));
                    elementTd.eq(2).html('<div class="progress-list">'+($("#template_progress").render({progress:data.ratioBudget})) +'</div>');
                },
                drawCallback: function () {
                    $.ExtNumeric.initAll();
                    $.ExtAssist.urlParam.setUrl();
                }
            };
            if(!$.ExtCommon.isCheckEmpty(self.pageData.listParam.offset)){options.displayStart=self.pageData.listParam.offset}
            self.extObject.table=self.elementModel.table.ExtDataTable(options);
        },
        bindList:function(){
            var data=this.model.get("dataPageList");
            this.extObject.table.ajaxWholeRefreshData(data);
        },
        refreshList: function () {this.extObject.table.refresh()}
    });
    var viewModel=function(){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({el: "#container_bodyMain",collection:this.collection,model:this.model});
    };
    viewModel.prototype={
        initView:function () {
            this.view.initMain();
        }
    };
    return viewModel;
});